package app.fugema.shop.main.order.adapter;

import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ItemShopingOrderStroeGoodsViewBinding;
import app.fugema.shop.main.invoice.model.SubOrderGoodsBean;
import app.fugema.shop.main.invoice.view.InvoiceInfoActivity;
import app.fugema.shop.main.order.view.CouponSelectActivity;

public class ShoppingConfirmOrderGoodsMultiType extends BaseItemMultiType<SubOrderGoodsBean, ItemShopingOrderStroeGoodsViewBinding>
{

    @Override
    public int getLayoutId()
    {
        return R.layout.item_shoping_order_stroe_goods_view;
    }

    @Override
    public void onCreateViewHolder(ItemShopingOrderStroeGoodsViewBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);
        binding.rvRecyclerViewGoods.setAdapter(new ConfirmOrderGoodsAdapter());
    }

    @Override
    public void onBindViewHolder(ItemShopingOrderStroeGoodsViewBinding binding, int position, BaseViewHolder viewHolder, SubOrderGoodsBean bean) {
        super.onBindViewHolder(binding, position, viewHolder, bean);
    }

    @Override
    public void onBindViewHolder(ItemShopingOrderStroeGoodsViewBinding binding, int position, SubOrderGoodsBean bean)
    {
        super.onBindViewHolder(binding, position, bean);
        /*
        if (position == 1)
        {
            binding.clItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_10));
            binding.clItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_10));
        } else {
            binding.clItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_0));
            binding.clItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_0));
        }
        */
        if( binding.rvRecyclerViewGoods.getAdapter() instanceof ConfirmOrderGoodsAdapter)
        {
            ConfirmOrderGoodsAdapter goodsAdapter =   (ConfirmOrderGoodsAdapter)binding.rvRecyclerViewGoods.getAdapter();

            goodsAdapter.setDataList( bean.getCartItemModels() );
        }
        // 店铺订单备注
        binding.etRemark.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s)
            {
                String mRemark = s.toString().trim();
                if(UIUtils.isNotEmpty( mRemark ))
                {
                    //店铺 订单备注
                    bean.setRemark( mRemark );
                }
                else
                {
                    bean.setRemark("");
                }
            }
        });

        //显示 税率 ( 包含发票信息时 显示  )   税率 计算放到 set 方法中
        if( UIUtils.isNotEmpty( bean.getSimpleInvoice() ) )
        {
            int rate = bean.getSimpleInvoice().getRate();
            if( bean.getSimpleInvoice().getInvoicetype() == 2  )
            {
                ;
                //显示发票信息
                //"税率 3%，电子普通发票("+ bean.getSimpleInvoice().getInvoicecontext() +")"
                binding.tvFapiaoMsg.setText( new StringBuffer().append("税率").append(rate).append("%，电子普通发票(").append(bean.getSimpleInvoice().getInvoicecontext()).append(")").toString() );
                binding.rlInvoiceRateMoney.setVisibility(View.VISIBLE);
                String rateMoney = bean.getSimpleInvoice().getRateMoney();
                //税费
                binding.tvInvoiceMoney.setText("¥"+rateMoney);
                LogUtil.e( "计算添加税费"+rateMoney );
                //bean.getSimpleInvoice().setRateMoney( rateMoney );
            }
            else if(bean.getSimpleInvoice().getInvoicetype() == 3)
            {
                //显示发票信息
                //"税率 3%，增值税发票("+ bean.getSimpleInvoice().getInvoicecontext() +")"
                binding.tvFapiaoMsg.setText(new StringBuffer().append("税率").append(rate).append("%，增值税发票(").append(bean.getSimpleInvoice().getInvoicecontext()).append(")").toString() );
                binding.rlInvoiceRateMoney.setVisibility(View.VISIBLE);
                String rateMoney = bean.getSimpleInvoice().getRateMoney();
                //Float rateMoney = MoneyUtils.mul(Float.valueOf(""+bean.getShopTotalWithoutFreight()), 0.01f*rate );
                //税费
                binding.tvInvoiceMoney.setText("¥"+rateMoney);
                LogUtil.e( "计算添加税费"+rateMoney );
                //bean.getSimpleInvoice().setRateMoney( rateMoney );
            }
            else
            {
                binding.tvFapiaoMsg.setText("不需要发票");
                binding.rlInvoiceRateMoney.setVisibility(View.GONE);
                bean.getSimpleInvoice().setRateMoney( "0" );
            }
        }
        else
        {
            binding.tvFapiaoMsg.setText("不需要发票");
            binding.rlInvoiceRateMoney.setVisibility(View.GONE);
        }


        //显示优惠券优惠
        if(  UIUtils.isNotEmpty( bean.getOneCoupons() ) )
        {
            //使用优惠券时
            binding.rlCouponView.setVisibility(View.VISIBLE);
            binding.tvShopCouponMoney.setText( new StringBuffer().append("-¥").append( bean.getOneCoupons().getBasePrice() ).toString() );
        }
        else
        {
            //不使用优惠券时
            binding.rlCouponView.setVisibility(View.GONE);
        }

        //显示满减优惠
        if( bean.getFullDiscount() > 0  )
        {
            binding.rlFulldiscountView.setVisibility(View.VISIBLE);
            binding.tvFullDiscountMoney.setText( new StringBuffer().append("-¥").append( bean.getFullDiscount() ).toString() );
        }
        else
        {
            binding.rlFulldiscountView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, SubOrderGoodsBean bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        switch (view.getId()) {
            case R.id.tv_fapiao_msg:
                LogUtil.e("MultiType 填写发票");
                //跳转 填写发票
                InvoiceInfoActivity.start(view.getContext(), bean.getSimpleInvoice());
                //LiveEventBus.get(AppConstants.EventKey.CONFIRM_ORDER_CLICK,Integer.class).post(R.id.tv_fapiao_msg);
                break;

            case R.id.rl_coupon_view:
                LogUtil.e("MultiType 选择优惠券");
                if ( UIUtils.isNotEmpty( bean.getBaseCoupons() ) )
                {
                    CouponSelectActivity.start(view.getContext(),bean.getBaseCoupons());
                }
                break;
        }
    }

}
